<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ details.title }}</h1>

      <!-- 用户信息 -->
      <van-cell
        center
        :title="details.aut_name"
        :label="setTime(details.pubdate)"
      >
        <template #icon>
          <img :src="details.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              type="info"
              size="mini"
              v-if="details.is_followed === true"
              @click="noFollowed"
              >已关注</van-button
            >
            <van-button
              v-else
              icon="plus"
              type="info"
              size="mini"
              plain
              @click="followed"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="details.content"></div>

      <!-- 分割线 -->
      <van-divider id="like-box">End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          v-if="details.attitude === 1"
          icon="good-job"
          type="danger"
          size="small"
          @click="noCollected"
          >已点赞</van-button
        >
        <van-button
          v-else
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          @click="collected"
          >点赞</van-button
        >
      </div>
    </div>
    <div class="cxh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        offset="50"
        immediate-check
      >
        <CommentList
          v-for="(item, index) in commentList"
          :key="index"
          :commentList="item"
        />
      </van-list>
    </div>
    <!-- 底部添加评论区域 - 1 -->
    <div class="add-cmt-box van-hairline--top" v-if="show">
      <van-icon name="arrow-left" size="0.48rem" @click="$router.back()" />
      <div class="ipt-cmt-div" @click="show = false">发表评论</div>
      <div class="icon-box">
        <van-badge
          :content="totalCount"
          :max="99"
          v-show="totalCount !== 0"
          @click.native="Fn"
        >
          <van-icon name="comment-o" size="0.53333334rem" />
        </van-badge>
        <van-icon
          name="star-o"
          size="0.53333334rem"
          @click="collectionFn"
          v-if="details.is_collected"
        />
        <van-icon
          name="star"
          size="0.53333334rem"
          @click="collectionFn"
          v-else
        />
        <van-icon name="share-o" size="0.53333334rem" />
      </div>
    </div>

    <!-- 底部添加评论区域 - 2 -->
    <div class="cmt-box van-hairline--top" v-else>
      <textarea
        placeholder="友善评论、理性发言、阳光心灵"
        v-gfocus
        v-model="text"
        @keydown.enter="release"
        @blur="blurFn"
      ></textarea>
      <van-button type="default" @click="release">发布</van-button>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
import CommentList from './CommentList.vue'
import { Toast } from 'vant'
import { articleDetails, followed, noFollowed, collected, noCollected, getComment, setComment } from '@/api/details.js'
export default {
  components: {
    CommentList
  },
  data () {
    return {
      details: {},
      is_collected: false,
      commentList: [],
      text: '',
      show: true,
      loading: false,
      finished: false,
      last_id: '',
      totalCount: ''
    }
  },
  methods: {
    // 收藏
    collectionFn () {
      if (this.details.is_collected) {
        this.details.is_collected = false
        Toast.success('收藏成功')
      } else {
        this.details.is_collected = true
        Toast.success('取消收藏成功')
      }
    },
    blurFn () {
      setTimeout(() => { this.show = true })
    },
    Fn () {
      document.getElementById('like-box').scrollIntoView({ behavior: 'smooth' })
    },
    async release () {
      try {
        const resp = await setComment(this.details.art_id, this.text)
        this.commentList.unshift(resp.data.new_obj)
        this.show = true
        this.text = ''
        this.totalCount++
        console.log(resp)
        Toast.success('发布成功')
      } catch (e) {
        Toast.fail('发布失败')
      }
    },
    setTime (a) {
      moment.locale('zh-cn')
      return moment(a).from(moment(new Date()))
    },

    //   关注
    async followed () {
      try {
        this.details.is_followed = true
        const resp = await followed(this.details.aut_id)
        console.log(resp)
        Toast.success('关注成功')
      } catch (e) {
        Toast.fail('关注失败')
      }
    },
    async noFollowed () {
      try {
        this.details.is_followed = false
        const resp = await noFollowed(this.details.aut_id)
        console.log(resp)
        Toast.success('取消关注成功')
      } catch (e) {
        Toast.fail('取消关注失败')
      }
    },
    // 点赞
    async collected () {
      try {
        this.details.attitude = 1
        console.log(this.details.aut_id)
        const resp = await collected(this.details.art_id)
        console.log(resp)
        Toast.success('点赞成功')
      } catch (e) {
        Toast.fail('点赞失败')
      }
    },
    async noCollected () {
      try {
        this.details.attitude = -1
        const resp = await noCollected(this.details.art_id)
        console.log(resp)
        Toast.success('取消点赞成功')
      } catch (e) {
        Toast.fail('取消点赞失败')
      }
    },
    // 上拉刷新
    async onLoad () {
      if (this.commentList.length > 0) {
        const res = await getComment({ source: this.id, offset: this.last_id })
        this.last_id = res.data.last_id
        if (this.last_id === null) {
          this.finished = true
          return
        }
        this.commentList = [...this.commentList, ...res.data.results]
      }
      this.loading = false
    }
  },
  async created () {
    const id = this.$route.query.id
    // console.log(id)
    const resp = await articleDetails(id)
    this.details = resp.data

    // console.log(resp) // 文章详情的结果页
    const res = await getComment({ source: this.details.art_id })
    this.commentList = res.data.results
    this.totalCount = res.data.total_count
    // console.log(res)
  },
  computed: {
    id () { return this.$route.query.id }
  }
}
</script>

<style scoped lang="less">
.cxh {
  margin-bottom: 40px;
}
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}

/*美化 - 文章详情 - 底部的评论页面 */
// 外层容器
.art-cmt-container-1 {
  padding-bottom: 46px;
}
.art-cmt-container-2 {
  padding-bottom: 80px;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
</style>
